package io.gitee.tziye.core.kv.model;

import io.gitee.tziye.RenaiClient;
import io.gitee.tziye.core.kv.ListOperator;
import io.gitee.tziye.core.kv.RenaiKey;
import io.gitee.tziye.core.kv.RenaiValue;
import io.gitee.tziye.enums.KvType;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

public class RenaiList<T> extends RenaiKVCollection<T> {

    private final ListOperator listOperator;

    public RenaiList(RenaiClient renaiClient, String key, Class<T> clazz, Integer expireSeconds) {
        super(renaiClient, key, clazz, KvType.List);
        listOperator = renaiClient.list();
        listOperator.saveKey(key, expireSeconds);
    }

    @Override
    public RenaiKey key() {
        return listOperator.key(key);
    }

    @Override
    public void expire(int expireSeconds) {
        listOperator.expire(key, expireSeconds);
    }

    @Override
    public boolean exists() {
        return listOperator.exists(key);
    }

    @Override
    public boolean delete() {
        return listOperator.delete(key);
    }

    @Override
    public List<RenaiValue> values(String key) {
        return listOperator.values(key);
    }

    @Override
    public int size() {
        return listOperator.size(key);
    }

    @Override
    public boolean contains(T value) {
        return listOperator.contains(key, value);
    }

    @Override
    public int clear() {
        return listOperator.clear(key);
    }

    @Override
    public boolean remove(T value) {
        return listOperator.remove(key, value);
    }

    @Override
    public int removeValues(List<T> values) {
        return listOperator.removeValues(key, values);
    }

    public List<T> members() {
        return listOperator.members(key, clazz);
    }

    public void add(T value) {
        listOperator.add(key, value);
    }

    public void add(Collection<T> values) {
        listOperator.add(key, values);
    }

    public void addIfAbsent(T value) {
        listOperator.addIfAbsent(key, value);
    }

    public void addIfAbsent(Collection<T> values) {
        listOperator.addIfAbsent(key, values);
    }

    public void insert(int idx, T value) {
        listOperator.insert(key, idx, value);
    }

    public void insert(int idx, Collection<T> values) {
        listOperator.insert(key, idx, values);
    }

    public T get(int idx) {
        List<T> list = listOperator.get(key, idx, 1, clazz);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public List<T> get(int start, int count) {
        return listOperator.get(key, start, count, clazz);
    }

    public T revGet(int idx) {
        return listOperator.revGet(key, idx, clazz);
    }

    public List<T> revGet(int start, int count) {
        return listOperator.revGet(key, start, count, clazz);
    }

    public T pop(int idx) {
        return listOperator.pop(key, idx, clazz);
    }

    public List<T> pop(int idx, int count) {
        return listOperator.pop(key, idx, count, clazz);
    }

    public T revPop(int idx) {
        return listOperator.revPop(key, idx, clazz);
    }

    public List<T> revPop(int idx, int count) {
        return listOperator.revPop(key, idx, count, clazz);
    }

    public int delete(int start, int count) {
        return listOperator.delete(key, start, count);
    }

    public int revDelete(int start, int count) {
        return listOperator.revDelete(key, start, count);
    }

    public int replace(T oldValue, T newValue) {
        return listOperator.replace(key, oldValue, newValue);
    }

    public List<Integer> indexOf(T value) {
        return listOperator.indexOf(key, value);
    }

    public Integer firstIndexOf(T value) {
        return listOperator.firstIndexOf(key, value);
    }

    public Integer lastIndexOf(T value) {
        return listOperator.lastIndexOf(key, value);
    }

}
